package com.hightemperator.controller;

import com.hightemperator.po.*;
import com.hightemperator.service.HomePageService;
import com.hightemperator.util.GetIp;
import com.hightemperator.util.LocalResponseBody;
import com.hightemperator.util.getOfToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/homepage")
public class HomePageController {

    @Autowired
    public HomePageService homePageService;

    /**
     * 首页banner
     *
     * @return
     */
    @RequestMapping("/banner")
    public LocalResponseBody getBanner(HttpServletRequest request) {
        String path2 = new GetIp().getServerIp() + ":" + request.getLocalPort();
        System.out.println(path2);
        List<Banner> banners = homePageService.getBanner();
        for (Banner banner : banners) {
            banner.setBannerpath(UserController.handleRequestUrl(banner.getBannerpath(), request, "picture/banner"));
        }
        return new LocalResponseBody("操作成功").doSuccess(banners);
    }


    /**
     * 获取首页vip列表
     *
     * @return
     */
    @RequestMapping("/getVIPList")
    public LocalResponseBody getVIPList() {
        List<SysUser> sysUsers = homePageService.getVIPUser();
        return new LocalResponseBody("操作成功").doSuccess(sysUsers);
    }


    /**
     * 首页及其他页面的查询 动态和活动 包括高级查询
     *
     * @param request     返回对象
     * @param type        0为活动1为动态
     * @param id          被查询用户id
     * @param isUser      是否为用户自己查询自己的动态活动
     * @param city        高级检索中的城市
     * @param sex         高级检索中的性别
     * @param age         高级检索中的年龄
     * @param hobby       高级检索中的爱好
     * @param heightMin   高级检索中的身高下限
     * @param heightMax   高级检索中的身高上限
     * @param weightMin   高级检索中的重量下限
     * @param weightMax   高级检索中的重量上限
     * @param nativeplace 高级检索中的籍贯
     * @return 活动或动态的数据
     */
    @RequestMapping("activityAndCircle")
    public LocalResponseBody getActivityOrTrend(HttpServletRequest request,
                                                @RequestParam(value = "type") String type,
                                                @RequestParam(value = "id", required = false, defaultValue = "") Integer id,
                                                @RequestParam(value = "userId", required = false, defaultValue = "") Integer userId,
                                                @RequestParam(value = "isUser", required = false, defaultValue = "") String isUser,
                                                @RequestParam(value = "city", required = false) String city,
                                                @RequestParam(value = "sex", required = false) String sex,
                                                @RequestParam(value = "age", required = false) String age,
                                                @RequestParam(value = "hobby", required = false) String hobby,
                                                @RequestParam(value = "heightMin", required = false) String heightMin,
                                                @RequestParam(value = "heightMax", required = false) String heightMax,
                                                @RequestParam(value = "weightMin", required = false) String weightMin,
                                                @RequestParam(value = "weightMax", required = false) String weightMax,
                                                @RequestParam(value = "nativeplace", required = false) String nativeplace,
                                                @RequestParam(value = "begin") Integer begin,
                                                @RequestParam(value = "page") Integer end) {

        Integer uId = null;
        String token = request.getParameter("token");
        if (token != null && token.length() > 0) {
            uId = getOfToken.getUserMessageString(token, request);
        }
        begin = begin * end;

        if (type != null && !(type.equals("0") || type.equals("1")))
            return new LocalResponseBody("操作失败").doSuccess();

        //当type为0的时候 获取的信息为活动
        if (type.equals("0")) {
            List<ActivityAndUserMessage> activityUserMessageList = null;
            //查询数据为指定用户的活动的具体信息
            if (id != null || isUser.equals("1")) {
                if (uId != null) {
                    id = (id == null || id.equals("")) && isUser.equals("1") ? uId : id;
                }
                activityUserMessageList = homePageService.getActivityListById(id, begin, end);
            } else {
                //查询高级搜索或者普通搜索的活动列表
                activityUserMessageList = homePageService.getAcitvityList(uId, city, sex, age, hobby, heightMin,
                        heightMax, weightMin, weightMax, nativeplace, begin, end);
            }
            for (ActivityAndUserMessage activityAndUserMessage : activityUserMessageList) {
                ActivityUserMessage activityUserMessage = activityAndUserMessage.getActivityUserMessage();
                activityUserMessage.setHeadimage(UserController.handleRequestUrl(activityUserMessage.getHeadimage(), request, "picture/userheadimage"));
                activityAndUserMessage.setActivityUserMessage(activityUserMessage);
            }
            return new LocalResponseBody("操作成功").doSuccess(activityUserMessageList);
        }
        //当type为1的时候 获取的信息为动态
        else {
            List<CircleAndUserMessage> circleAndUserMessageList = null;
            //查询数据为指定用户的动态的具体信息
            if (id != null || isUser.equals("1")) {
                if (uId != null) {
                    id = (id == null || id.equals("")) && isUser.equals("1") ? uId : id;
                }
                circleAndUserMessageList = homePageService.getCircleListById(id, begin, end);
            } else {
                //查询高级搜索或者普通搜索的动态列表
                circleAndUserMessageList = homePageService.getCircleMessageList(uId, city, sex, age, hobby, heightMin,
                        heightMax, weightMin, weightMax, nativeplace, begin, end);
            }
            for (CircleAndUserMessage circleAndUserMessage : circleAndUserMessageList) {
                circleAndUserMessage.setCircleimage(UserController.handleRequestUrl(circleAndUserMessage.getCircleimage(), request, "circlePicture"));
                ActivityUserMessage activityUserMessage = circleAndUserMessage.getActivityUserMessage();
                activityUserMessage.setHeadimage(UserController.handleRequestUrl(activityUserMessage.getHeadimage(), request, "picture/userheadimage"));
                circleAndUserMessage.setActivityUserMessage(activityUserMessage);
            }
            return new LocalResponseBody("操作成功").doSuccess(circleAndUserMessageList);
        }
    }

    /**
     * 加入活动并相互关注
     *
     * @param joinActivity
     * @return
     */
    @RequestMapping("joinActivityAndAttention")
    public LocalResponseBody joinActivityAndAttention(JoinActivity joinActivity) {
        int successCode = homePageService.updateJoinActivity(joinActivity);
        if (successCode == 0) {
            return new LocalResponseBody("操作失败").doFail();
        }
        return new LocalResponseBody("操作成功").doSuccess();
    }

}